'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _some = require('lodash/some');

var _some2 = _interopRequireDefault(_some);

var _pick = require('lodash/pick');

var _pick2 = _interopRequireDefault(_pick);

var _normalizeKeys = require('../utils/normalizeKeys');

var _normalizeKeys2 = _interopRequireDefault(_normalizeKeys);

var _responseCodes = require('./consts/responseCodes');

var ResponseCodes = _interopRequireWildcard(_responseCodes);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var SuccessCodes = (0, _pick2.default)(ResponseCodes, ['SUCCESS']);

/**
 * 自定义response类
 * @class Response
 * @memberOf fe-lib.FetchRequest
 * @inner
 */

var CustomResponse = function () {
  CustomResponse.isSuccess = function isSuccess(responseCode) {
    return (0, _some2.default)(SuccessCodes, function (value) {
      return value === responseCode;
    });
  };

  CustomResponse.parseHeaders = function parseHeaders(res) {
    return (0, _normalizeKeys2.default)(res.headers || { responseCode: ResponseCodes.UNKNOWN_ERROR, responseMessage: '' });
  };

  CustomResponse.parseBody = function parseBody(res) {
    return (0, _normalizeKeys2.default)(res.body || {});
  };

  CustomResponse.resetStatic = function resetStatic(methods) {
    Object.keys(methods).forEach(function (key) {
      return CustomResponse[key] = methods[key];
    });
  };

  function CustomResponse(res) {
    _classCallCheck(this, CustomResponse);

    this.headers = CustomResponse.parseHeaders(res);
    this.body = CustomResponse.parseBody(res);
    this.code = this.headers.responseCode;
    this.message = this.headers.responseMessage;
    this.success = CustomResponse.isSuccess(this.code);
  }

  return CustomResponse;
}();

exports.default = CustomResponse;